package com.jason.rxjavatest.retrofit;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jason.rxjavatest.R;
import com.jason.rxjavatest.retrofit.bean.PhoneAPI;
import com.jason.rxjavatest.retrofit.bean.PhoneResult;
import com.jason.rxjavatest.retrofit.interf.PhoneService;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 *
 * @Author: Jason
 * @Date: 16/5/16
 * @Time: 11:27
 */

public class RetrofitTest01 extends AppCompatActivity {


    private static final String TAG = RetrofitTest01.class.getSimpleName();

    @Bind(R.id.result)
    TextView mResult;
    @Bind(R.id.edittext)
    EditText mEdittext;
    @Bind(R.id.edittext_wrapper)
    TextInputLayout mEdittextWrapper;
    @Bind(R.id.query)
    Button mQuery;

    private PhoneAPI mPhoneAPI;
    private PhoneService mPhoneService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_retrofit);
        ButterKnife.bind(this);
        mResult.requestFocus();

        mPhoneAPI = PhoneAPI.getApi();
        mPhoneService = mPhoneAPI.getPhoneService();

    }

    /**
     * 查询手机号码
     */
    private void queryCityName() {
        mResult.setText("");
        // 1.创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create()) // 解析方法
                .baseUrl(PhoneAPI.BASE_URL) // 主机url
                .build();

        // 2.创建访问API的请求
        PhoneService phoneService = retrofit.create(PhoneService.class);
        Call<PhoneResult> call = phoneService.getResult(PhoneAPI.API_KEY, mEdittext.getText().toString().trim());


        call.enqueue(new Callback<PhoneResult>() {
            @Override
            public void onResponse(Call<PhoneResult> call, Response<PhoneResult> response) {
                PhoneResult phoneResult = response.body();
                if (phoneResult != null) {
                    PhoneResult.RetDataEntity retData = phoneResult.getRetData();
                    mResult.setText("地址: " + retData.getProvince() + retData.getCity());
                } else {
                    Log.e(TAG, "phoneResule===null");
                }

            }

            @Override
            public void onFailure(Call<PhoneResult> call, Throwable t) {

            }
        });
    }

    private void queryByRxJava() {
        mResult.setText("");
        if (!TextUtils.isEmpty(mEdittext.getText().toString().trim())) {

            mPhoneService.getPhoneResult(PhoneAPI.API_KEY, mEdittext.getText().toString().trim())
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<PhoneResult>() {
                        @Override
                        public void call(PhoneResult phoneResult) {
                            if (phoneResult != null && phoneResult.getErrNum() == 0) {
                                PhoneResult.RetDataEntity retData = phoneResult.getRetData();
                                mResult.append("地址: " + retData.getCity());
                            }
                        }
                    });

        } else {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
        }

    }


    @OnClick({R.id.query, R.id.RxJavaQuery, R.id.duokan})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.query:
                if (!TextUtils.isEmpty(mEdittext.getText().toString().trim())) {
                    queryCityName();
                } else {
                    Toast.makeText(this, "请填写手机号码", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.RxJavaQuery:
                queryByRxJava();
                break;

            case R.id.duokan:
                startActivity(new Intent(this, RetrofitTest02.class));
                break;
        }
    }
}
